package com.mdd.front.service.leasing.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.front.service.leasing.ILeasingSchemeDetailsFrontService;
import com.mdd.front.service.leasing.ILeasingSchemeFrontService;
import com.mdd.front.vo.leasing.LeasingSchemeVo;
import com.mdd.product.entity.LeasingScheme;
import com.mdd.product.mapper.LeasingSchemeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 租赁方案实现类
 *
 * @author LikeAdmin
 */
@Service
public class LeasingSchemeFrontServiceImpl implements ILeasingSchemeFrontService {

    @Resource
    LeasingSchemeMapper leasingSchemeMapper;
    @Resource
    ILeasingSchemeDetailsFrontService iLeasingSchemeDetailsFrontService;

    /**
     * 租赁方案列表
     * @param spuId 商品id
     * @return List<LeasingSchemeListedVo>
     * @author LikeAdmin
     */
    @Override
    public List<LeasingSchemeVo> list(Long spuId,Double riskScore) {
        LambdaQueryWrapper<LeasingScheme> leasingWrapper = new LambdaQueryWrapper<>();
        leasingWrapper.eq(LeasingScheme::getSpuId, spuId);
        List<LeasingScheme> leasingSchemes = leasingSchemeMapper.selectList(leasingWrapper);

        List<LeasingSchemeVo> list = new LinkedList<>();
        for (LeasingScheme item : leasingSchemes) {
            LeasingSchemeVo vo = new LeasingSchemeVo();
            BeanUtils.copyProperties(item, vo);
            vo.setLeasingSchemeDetailsVos(iLeasingSchemeDetailsFrontService.list(spuId,riskScore));
            list.add(vo);
        }
        return list;
    }

    /**
     * 租赁方案详情
     * @param id 主键参数
     * @return OmsLeasingScheme
     * @author LikeAdmin
     */
    @Override
    public LeasingSchemeVo detail(Long id) {
        LeasingScheme model = leasingSchemeMapper.selectById(id);

        Assert.notNull(model, "租赁方案数据不存在");

        LeasingSchemeVo vo = new LeasingSchemeVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    @Override
    public String getFirstAmountStr(Long spuId) {
        return iLeasingSchemeDetailsFrontService.getFirst(spuId).getAmountStr();
    }
}
